import router from './router'
import store from './store'

//白名单名单
const whiteList = ['/reg','/login']

router.beforeEach((to,from,next)=>{
  // console.log('所有的页面在跳转之前都会经过这里');
  // console.log('去哪里',to.path)
  // console.log('从哪里来',from.path)
  // 控制访问权限
  // 1.判断是否有token
  const token = store.state.user.token
  if(token){
    // console.log('已登录')
    if(to.path === '/login'){
      // console.log('已经登录首页')
      next('/')
    }else {
      // console.log('不是去登录首页,放行');
      next()
    }
  }else {
    // console.log('没有登录')
    if(whiteList.includes(to.path)){
      // console.log('放行');
      next()
    }else {
      // console.log('请先登录');
      next('/login')
    }
  }
})
